package com.gx.smart.building.ui.knowledge

import android.content.Intent
import android.view.ViewGroup
import com.gx.smart.base.BaseAdapter
import com.gx.smart.base.BaseMVVMRefreshActivity
import com.gx.smart.building.R
import com.gx.smart.building.databinding.ActivityKnowledgeArticleBinding
import com.gx.smart.building.ui.knowledge.adapter.ArticleAdapter
import com.gx.smart.building.ui.knowledge.viewmodel.KnowledgeViewModel
import com.gx.smart.lib.http.api.response.Knowledge
import com.scwang.smartrefresh.layout.api.RefreshLayout

class KnowledgeArticleActivity : BaseMVVMRefreshActivity<KnowledgeViewModel>(),
    BaseAdapter.OnItemClickListener<Knowledge> {
    private lateinit var viewBinding: ActivityKnowledgeArticleBinding
    private lateinit var adapter: ArticleAdapter
    override fun onBindViewModel(): Class<KnowledgeViewModel> = KnowledgeViewModel::class.java

    override fun onBindLayout(): Int = R.layout.activity_knowledge_article

    override fun getToolbarTitle(): String = "文章列表"

    override fun initContentView(root: ViewGroup?) {
        viewBinding = ActivityKnowledgeArticleBinding.inflate(layoutInflater, root, true)
        adapter = ArticleAdapter(this)
        adapter.setItemClickListener(this)
        initView(viewBinding.recyclerView, adapter, viewBinding.refreshLayout)
    }

    override fun onRefresh(refreshlayout: RefreshLayout) {
        super.onRefresh(refreshlayout)
        doAction()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        super.onLoadMore(refreshLayout)
        doAction()
    }

    private fun doAction() {
        val baseTypeId = intent.getStringExtra("id")
        viewModel.queryKnowledgeList(this.currentPage, baseTypeId!!).observe(this) { list ->
            handleResult(list)
        }
    }

    override fun onResume() {
        super.onResume()
        viewBinding.refreshLayout.autoRefresh()
    }

    override fun onItemClick(item: Knowledge, position: Int) {
        startActivity(
            Intent(this, KnowledgeArticleDetailActivity::class.java)
                .apply {
                    putExtra("id", item.id)
                }
        )
    }
}